package com.prs.crm.flow.adapter.impl;

import java.io.Writer;
import java.text.DecimalFormat;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.prs.crm.context.Constrains;
import com.prs.crm.domain.flow.WorkFlow;
import com.prs.crm.domain.flow.WorkFlowNode;
import com.prs.crm.domain.flow.WorkFlowTransition;
import com.prs.crm.flow.adapter.IFlowXmlWriter;

public class FlowXmlWriterImpl implements IFlowXmlWriter{
	
	 private static final Logger log = Logger.getLogger(FlowXmlWriterImpl.class);
	 
		public void  write(WorkFlow flow,Writer writer)
		{
			Document document = DocumentHelper.createDocument();
			document.setXMLEncoding("utf-8");
			
			String nodetype=null;

			DecimalFormat df = new DecimalFormat("#"); 
			Element root = document.addElement("WorkflowProcess");
			int maxid = 0;
			//write flow node
			Element activies = root.addElement("Activities");
			for (WorkFlowNode node:flow.getNodes())
			{
				Element anode = activies.addElement("Activitie");
				anode.addAttribute("id", node.getId().toString());
				if (node.getId()>maxid)maxid = node.getId();
				switch (node.getType().intValue())
				{
					case Constrains.WORKFLOW_STARTNODE:nodetype="START_NODE";break;
					case Constrains.WORKFLOW_NODE:nodetype="NODE";break;
					case Constrains.WORKFLOW_ENDNODE:nodetype="END_NODE";break;
					case Constrains.WORKFLOW_FORKNODE:nodetype="FORK_NODE";break;
					case Constrains.WORKFLOW_JOINNODE:nodetype="JOIN_NODE";break;
					default:nodetype="NODE";break;
				}
				anode.addAttribute("type", nodetype);
				anode.addAttribute("name", node.getName());
				anode.addAttribute("xCoordinate", df.format(node.getExtra().getXcoordinate()));
				anode.addAttribute("yCoordinate", df.format(node.getExtra().getYcoordinate()));
				anode.addAttribute("width",String.valueOf(node.getExtra().getWidth()));
				anode.addAttribute("height", String.valueOf(node.getExtra().getHeight()));
			}
			//write flow transition
			Element transitions = root.addElement("Transitions");
			for (WorkFlowTransition transition :flow.getTransitions())
			{
				Element trans = transitions.addElement("Transition");
				trans.addAttribute("id",String.valueOf(++maxid));
				//trans.addAttribute("id",transition.getId().toString());
				if (trans.getName()!=null)
					trans.addAttribute("name", transition.getName());
				else
					trans.addAttribute("name", "");
				trans.addAttribute("from", transition.getStartnode().getId().toString());
				trans.addAttribute("to", transition.getEndnode().getId().toString());
			}
			
			try
			{
			 document.write(writer);
			}
			catch (Exception e)
			{
				log.error(e);
			}
	/*		<?xml version="1.0" encoding="UTF-8"?>
			<WorkflowProcess>
			  <Activities>
			    <Activitie id="1" type="START_NODE" name="瀵偓婵� xCoordinate="0" yCoordinate="100" width="80" height="30"></Activitie>
			  </Activities>
			  <Transitions>
			    <Transition id="12" name="鏉╃偞甯� from="1" to="2"></Transition>
			  </Transitions>
			</WorkflowProcess>*/
		}
}
